# -*- coding: utf-8 -*-
# Created by liangfuting on 2020/6/21
# Copyright (c) 2020 liangfuting. All rights reserved.
from django.urls import reverse
from django.http import QueryDict


def memory_reverse_url(request,old_url,*args,**kwargs):
    #反向生成url,同时保留原搜索条件
    url = reverse(old_url,args=args,kwargs=kwargs)
    origin_params = request.GET.get('_filter')
    if origin_params:
        url = "%s?%s" % (url, origin_params)
    return url

def memory_url(request,name,*args,**kwargs):
    """"
    生成带有原搜索条件的URL,替换了原模板中的url
    """
    basic_url=reverse(name,args=args,kwargs=kwargs)
    if not request.GET:
        # 如果当前页面请求中存在参数
        return basic_url
    else:
        old_params = request.GET.urlencode()  # 获取页面中的参数
        query_dict = QueryDict(mutable=True)
        query_dict['_filter'] = old_params
        new_url = "%s?%s" % (basic_url, query_dict.urlencode())
        # print(new_url)
        return new_url